import { InjectableComponent } from '../../inject';
import UIManager from '../../view/UIManager';
import { ViewParams } from '../../view/ViewDefine';
import BaseViewMvpProxy, { IBaseViewProxy } from '../proxy/view/ViewMvpProxy';

export abstract class BaseView extends InjectableComponent {

    private _proxy: IBaseViewProxy = null;

    private _showing: boolean;

    private _vp: ViewParams = null;

    onLoad() {
        super.onLoad();
        this.getMvpProxy().injectPresenters();
    }

    onDestroy(): void {
        this.getMvpProxy().detachPresenters();
        super.onDestroy();
    }

    public triggerAdded(vp: ViewParams): void {
        this._vp = vp;
        this._showing = true;

        if (this.onAdded)
            this.onAdded();
    }

    public triggerRemoved(vp: ViewParams): void {
        this._showing = false;
        if (this.onRemoved)
            this.onRemoved();
    }

    public triggerBeforeRemove(vp: ViewParams): void {
        if (this.onBeforeRemove)
            this.onBeforeRemove();
    }

    public onAdded?(): void;
    public onRemoved?(): void;
    public onBeforeRemove?(): void


    public get isShowing(): boolean {
        return this._showing;
    }

    public removeSelf(isDestroy: boolean = true): void {
        if (this._vp) {
            this.injector.getInstance(UIManager).remove(this._vp.config, isDestroy);
        } else {

        }
    }

    private getMvpProxy() {
        if (null === this._proxy) {
            this._proxy = new BaseViewMvpProxy(this);
        }

        return this._proxy;
    }


}
